#include <libk/stdio.h>
#include <libk/x86.h>

unsigned long ticks = 0;

void timer_handler(void)
{
	ticks++;

	if (ticks % 500 == 0)
	{
		blink();
	}
	if (ticks % 10 == 0)
	{
		outb(0x20, 0x20);
	}
}

void timer_install()
{
	irq_install_handler(0, (void *)timer_handler);
}

void set_timer_freq(unsigned int hz)
{
	unsigned int divisor = 1193180 / hz;       /* Calculate our divisor */
	outb(0x43, 0x36);             /* Set our command byte 0x36 */
	outb(0x40, divisor & 0xFF);   /* Set low byte of divisor */
	outb(0x40, divisor >> 8);     /* Set high byte of divisor */
}

void timer_wait(unsigned long tix)
{
	unsigned long etix = ticks + tix;
	while (ticks < etix);
}
